package com.nathan.crm.controller;

import com.nathan.base.BaseController;
import com.nathan.base.ResultInfo;
import com.nathan.crm.enums.StateStatus;
import com.nathan.crm.query.SaleChanceQuery;
import com.nathan.crm.service.SaleChanceService;
import com.nathan.crm.utils.CookieUtil;
import com.nathan.crm.utils.LoginUserUtil;
import com.nathan.crm.vo.SaleChance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController {

    @Autowired
    private SaleChanceService saleChanceService;

    /*
        分页查询营销机会数据
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,Integer flag,HttpServletRequest request){

        //flag不为空且等于1，表示查询客户开发计划
        if (flag != null && flag == 1){
            //设置分配状态为已分配
            saleChanceQuery.setState(StateStatus.STATED.getType());
            //查询当前登录用户的id
            int id = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(id);
        }

        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    /*
        添加营销机会
     */
    @ResponseBody
    @PostMapping("add")
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){
        //获取cookie中的username
        String userName = CookieUtil.getCookieValue(request, "userName");
        saleChance.setCreateMan(userName);

        saleChanceService.addSaleChance(saleChance);

        return success("添加成功");
    }

    /*
        修改营销机会
     */
    @ResponseBody
    @PostMapping("update")
    public ResultInfo updateSaleChance(SaleChance saleChance){
        saleChanceService.updateSaleChance(saleChance);

        return success("修改成功");
    }

    /*
        跳转到sale_chance.ftl
     */
    @RequestMapping("index")
    public String index(){
        return "saleChance/sale_chance";
    }

    /*
        跟新营销机会的开发状态(DevResult)
     */
    @ResponseBody
    @RequestMapping("updateSaleChanceDevResult")
    public ResultInfo updateSaleChanceDevResult(Integer id,Integer devResult){
        saleChanceService.updateSaleChanceDevResult(id,devResult);

        return success("开发状态更新成功！");
    }

    /*
        跳转到机会添加或修改页面
     */
    @RequestMapping("addOrUpdateSaleChancePage")
    public String addOrUpdateSaleChancePage(Integer id,HttpServletRequest request){
        if (id != null){    //编辑操作
            //根据id查询数据
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(id);
            request.setAttribute("saleChance",saleChance);
        }

        return "saleChance/add_update";
    }

    /*
        批量删除营销机会
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids){
        saleChanceService.deleteSaleChance(ids);

        return success("删除成功！");
    }
}
